﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Point = Game1942.Library.Background.Point;

namespace Game1942.Library.View
{
    public class PlayerView : IObserver<Algo42>, IView
    {
        private Texture2D normal;
        private Texture2D turningRight;
        private Texture2D turningLeft;
        private Texture2D currentTexture;
        private Point position;
        private IDisposable unsubscriber;
        private Boolean visible;

        public PlayerView(Point aPosition)
        {
            position = aPosition;
            visible = true;
        }

        public void LoadContent(Texture2D normalTexture, Texture2D rightTexture, Texture2D leftTexture)
        {
            currentTexture = normal = normalTexture;
            turningRight = rightTexture;
            turningLeft = leftTexture;
        }

        public void Draw(SpriteBatch sprite)
        {
            sprite.Draw(currentTexture, new Rectangle(position.X, position.Y, Constants.Algo42.Width, Constants.Algo42.Height), Color.White);
        }

        public bool Visible()
        {
            return visible;
        }

        #region [Observer Methods]

        public void Subscribe(IObservable<Algo42> provider)
        {
            if (provider != null)
                unsubscriber = provider.Subscribe(this);
        }

        public void OnNext(Algo42 value)
        {
            position = value.ActualPosition;
        }

        public void OnError(Exception error)
        {
            //Do nothing
        }

        public void OnCompleted()
        {
           unsubscriber.Dispose();
        }

        #endregion

        public void SetNormalView()
        {
            currentTexture = normal;
        }

        public void SetLeftView()
        {
            currentTexture = turningLeft;
        }

        public void SetRightView()
        {
            currentTexture = turningRight;
        }
    }

}
